<!-- 字典管理 -->
<template>
    <ContentWrap v-if="state.detailBool">
            <div class="flex justify-between">
            <div class="flex aic">
                <div class="flex aic jcc fontz fonts16 fontw700">
                    字典列表
                </div>
            </div>
            <div class="">
                <ElInput v-model="state.searchData.keywords" style="width: 266px;height: 32px;margin-right: 10px;"
                    placeholder="请输入字典名称" :prefix-icon="Search" />
                <ElButton type="primary" :icon="Search" @click="onSearch">
                    {{ t('common.query') }}
                </ElButton>
                <ElButton :icon="Refresh" @click="onReset">
                    {{ t('common.reset') }}
                </ElButton>
                <ElButton type="primary" :icon="Plus" @click="onAdd">
                    {{ t('新增字典') }}
                </ElButton>
            </div>
        </div>
        <div class="" style="margin-top: 25px;">
            <Table v-model:pageSize="pageSize" v-model:currentPage="currentPage" :columns="allSchemas.tableColumns"
                :data="dataList" :loading="loading" :pagination="{ total: total }" @register="tableRegister"
                :headerCellStyle="headerCellStyle" :rowStyle="rowStyle" />
        </div>
    </ContentWrap>
    <DetailVue :data="state.detailData" @tableClicks="tableClicks" v-else/>
    <Dialog v-model="dialogVisible1" :title="state.dialogVisible1title" width="500">
        <el-form ref="ruleFormRef" :model="state.ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm"
            status-icon>
            <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="state.ruleForm.dictName" placeholder="请输入字典名称" />
            </el-form-item>
            <el-form-item label="字典类型" prop="dictType">
                <el-input v-model="state.ruleForm.dictType" placeholder="请输入字典类型" />
            </el-form-item>
            <!-- <el-form-item label="状态" prop="dictType">
                <el-radio-group v-model="state.ruleForm.name">
                    <el-radio label="启用" />
                    <el-radio label="停用" />
                </el-radio-group>
            </el-form-item> -->
            <el-form-item label="备注" prop="remark">
                <el-input v-model="state.ruleForm.remark" placeholder="请输入备注" :autosize="{ minRows: 2}" type="textarea" row="4" />
            </el-form-item>
            <el-form-item>
                <div class="flex jcfe w-[100%]">
                    <el-button type="primary" @click="submitForm(ruleFormRef)" v-loading="btn_loading">保存</el-button>
                    <el-button @click="resetForm(ruleFormRef)">取消</el-button>
                </div>
            </el-form-item>
        </el-form>
    </Dialog>
</template>
    
<script setup lang="tsx">
import  DetailVue  from "./detail.vue"
import { getlistApi, addTypeApi, editTypeApi, deteleTypeApi } from "@/api/common/index"
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTable'
import { Search, Refresh, Plus } from '@element-plus/icons-vue'
import { Dialog } from '@/components/Dialog'
import { useEmitt } from '@/hooks/event/useEmitt'
import { ElButton, ElInput, ElMessage, ElMessageBox, ElLink } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { ContentWrap } from '@/components/ContentWrap'
import { ref, reactive, unref } from "vue"
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
import { getlist } from "@/api/evaluationApi"
const { push } = useRouter()
const props = { multiple: true }
const btn_loading = ref(false)
interface RuleForm {
    dictName: string,
    dictType: string,
    remark: string
}
const ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules<RuleForm>>({
    dictName: [
        { required: true, message: '请输入字典名称！', trigger: 'blur' },
        { min: 1, max: 50, message: '不超过50字符！', trigger: 'blur' },
    ],
    dictType: [
        { required: true, message: '请输入字典类型！', trigger: 'blur' },
        { min: 1, max: 50, message: '不超过50字符！', trigger: 'blur' },
    ],
    remark: [
        { min: 1, max: 1000, message: '不超过1000字符！', trigger: 'blur' },
    ]
})
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            if (state.dialogVisible1title == '新增字典') {
                let obj = {
                    "createDept": "",
                    "createBy": "",
                    "createTime": "",
                    "updateBy": "",
                    "updateTime": "",
                    "params": {
                        "property1": {},
                        "property2": {}
                    },
                    "dictId": "",
                    "dictName": state.ruleForm.dictName,
                    "dictType": state.ruleForm.dictType,
                    "remark": state.ruleForm.remark
                }
                btn_loading.value = true
                addTypeApi(obj).then((res: any) => {
                    ElMessage({
                        type: 'success',
                        message: `${res.msg}`,
                    })
                    formEl.resetFields()
                    getList()
                    dialogVisible1.value = false
                    btn_loading.value = false
                }).catch((res: any) => {
                    ElMessage({
                        type: 'error',
                        message: `${res.msg}`,
                    })
                    btn_loading.value = false
                })
            } else {
                btn_loading.value = true
                editTypeApi(state.ruleForm).then((res: any) => {
                    ElMessage({
                        type: 'success',
                        message: `${res.msg}`,
                    })
                    formEl.resetFields()
                    getList()
                    dialogVisible1.value = false
                    btn_loading.value = false
                }).catch((res: any) => {
                    ElMessage({
                        type: 'error',
                        message: `${res.msg}`,
                    })
                    btn_loading.value = false
                })
            }
            console.log('submit!')
        } else {
            console.log('error submit!', fields)
        }
    })
}
const tableClicks=()=>{
    state.detailBool=true
}
const resetForm = (formEl: FormInstance | undefined) => {
    dialogVisible1.value = false
    if (!formEl) return
    formEl.resetFields()
}
const { t } = useI18n()
const dialogVisible1 = ref(false)//定义职位详情弹框
const state = reactive({
    searchData: {
        companyValue: "",
        jobValue: "",
        keywords: ""
    },//搜索参数
    react: true,//分页刷新
    dialogVisible1title: "新增字典",
    ruleForm: {} as any,
    detailBool:true,
    detailData:{} as any
})
// 表格基本信息
const crudSchemas = reactive<CrudSchema[]>([
    {
        field: 'dictName',
        label: t('字典名称'),
        search: {
            hidden: true
        }
    },
    {
        field: 'dictName',
        label: t('字典类型'),
        search: {
            component: 'Input'
        },
        form: {
            component: 'Input',
            colProps: {
                span: 24
            }
        },
        table: {
            slots: {
                default: (data: any) => {
                    return (
                        <>
                            <div class="colorBlue" >
                                <ElLink type="primary" underline={true} onClick={() => detailClick(data[0].row)}>
                                    {data[0].row.dictType ? data[0].row.dictType : ''}
                                </ElLink>
                            </div>
                        </>
                    )
                }
            }
        }
    },

    // {
    //     field: 'author',
    //     label: t('字典状态'),
    //     search: {
    //         hidden: true
    //     },
    //     table: {
    //         slots: {
    //             default: (data: any) => {
    //                 return (
    //                     <>
    //                         <div>{data[0].row.department_name ? data[0].row.department_name : ''}</div>
    //                     </>
    //                 )
    //             }
    //         }
    //     }
    // },
    {
        field: 'action',
        width: '260px',
        label: t('tableDemo.action'),
        search: {
            hidden: true
        },
        form: {
            hidden: true
        },
        detail: {
            hidden: true
        },
        table: {
            slots: {
                default: (data: any) => {
                    return (
                        <>
                            <ElButton link type="primary" onClick={() => action(data[0].row)}>
                                {t('编辑')}
                            </ElButton>
                            {/* <ElButton link type="primary" onClick={() => receat(data[0].row)}>
                                {t('查看')}
                            </ElButton> */}
                            <ElButton link type="danger" onClick={() => delData(data[0].row)}>
                                {t('删除')}
                            </ElButton>

                        </>
                    )
                }
            }
        }
    }
])

const { tableRegister, tableState, tableMethods } = useTable({
    fetchDataApi: async () => {
        const { currentPage, pageSize } = tableState
        const res: any = await getlistApi({
            pageNum: state.react ? 1 : unref(currentPage),
            pageSize: unref(pageSize),
            dictName: state.searchData.keywords
        })
        console.log(res);
        state.react = false
        return {
            list: res.rows,
            total: res.total
        }
    },
    fetchDelApi: async () => {
        const res = await delTableListApi(unref(ids))
        return !!res
    }
})
// 表格头部信息
const headerCellStyle: any = {
    "background": "#F2F6FC",
    "height": "48px"
}
// 
const rowStyle: any = {
    "height": "48px"
}
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods
getList()
useEmitt({
    name: 'getList',
    callback: (type: string) => {
        if (type === 'add') {
            currentPage.value = 1
        }
        getList()
    }
})
const { allSchemas } = useCrudSchemas(crudSchemas)
// 详情
const detailClick = (data: any) => {
    state.detailBool=false
    state.detailData={
        type:data.dictType
    }
    // push(`/dictionary/Dictionary_Detail-index/0?type=${data.dictType}`)
}
// 新增
const onAdd = () => {
    console.log("新增角色");
    state.ruleForm = {}
    dialogVisible1.value = true
    state.dialogVisible1title = t('新增字典')
}
const action = (row: any) => {
    console.log("职位详情", row);
    dialogVisible1.value = true
    state.ruleForm = row
    state.dialogVisible1title = t('编辑字典')
}
const delData = (row: any) => {
    ElMessageBox.confirm('确认删除该字典？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        // inputPattern:,
        draggable: true,
        type: 'warning',
    }).then(() => {
        deteleTypeApi(row.dictId).then((res: any) => {
            getList()
            ElMessage({
                type: 'success',
                message: `${res.msg}`,
            })
        }).catch((res: any) => {
            ElMessage({
                type: 'error',
                message: `${res.msg}`,
            })
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消操作',
        })
    })


}
// 查询
const onSearch = () => {
    console.log("查询");
    state.react = true
    getList()
}
// 重置
const onReset = () => {
    console.log("重置");
    state.searchData.companyValue = ""
    state.searchData.keywords = ""
    state.searchData.jobValue = ""
    state.react = true
    getList()
}
// api--------------------------------------------



</script>
    
<style lang="less" scoped></style>